package com.greenova.projector.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.greenova.projector.entity.Project;

@Transactional
@Repository
public class ProjectDaoImpl implements ProjectDao {

	@Autowired
	private HibernateTemplate hibernateTemplate;

	@Override
	public void addProject(Project project) {
		hibernateTemplate.save(project);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Project> listAllProjects() {
		return hibernateTemplate.find("from Project");
	}

	@Override
	public Project getProjectById(long projectId) {
		SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
		Session session = sessionFactory.openSession();
		Criteria crit = session.createCriteria(Project.class)
				.add(Restrictions.eq("id", projectId));
		Project project = (Project) crit.uniqueResult();
		session.close();
		return project;
	}

	@Override
	public void updateProject(Project project) {
		SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
		sessionFactory.openSession();
		hibernateTemplate.update(project);
		sessionFactory.close();
	}
	
	

}
